package com.core.pdf;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Configuration
//@ConfigurationProperties(prefix = "opc")
public class OpcUaConfig {

    private String endPointUrlData = null;
    //项目名称
    private String groupData = null;
    //打印机名称
    private String printNameData = null;
    //打印机名称
    private String printModeData = null;
    //地址
    private String fileName;
    //配置
    private Map<String, String> config = new HashMap();

    public String getEndPointUrl() {
        return endPointUrlData;
    }

    public void setEndPointUrl(String endPointUrl) {
        if (endPointUrlData == null) {
            this.endPointUrlData = endPointUrl;
        }
    }

    public String getGroup() {
        return groupData;
    }

    public void setGroup(String group) {
        if (groupData == null) {
            this.groupData = group;
        }
    }

    public String getPrintName() {
        return printNameData;
    }


    public String getPrintMode() {
        return printModeData;
    }


    public String getBarcodeTempDir(){
        return config.get("tempDir");
    }

    public void setPrintMode(String printMode) {
        if (printModeData == null) {
            this.printModeData = printMode;
        }
    }

    public void setPrintName(String printName) {
        if (printNameData == null) {
            this.printNameData = printName;
        }
    }

    public String getFileName() {
        return fileName;
    }


    public void setFileName(String fileName) {
        this.fileName = fileName;
        try {
            if (fileName == null || fileName.length() < 5) {
                return;
            }
            File file = new File(fileName);
            if (file.exists()) {
                List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
                if (lines != null) {
                    for (String line : lines) {
                        int idx = line.indexOf("=");
                        if (idx > 0 && line.length() > idx) {
                            String[] vs = line.split("=");
                            config.put(vs[0].trim(), vs[1].trim());
                        }
                    }
                    if (config.containsKey("endPointUrl")) {
                        this.endPointUrlData = config.get("endPointUrl");
                    }
                    if (config.containsKey("group")) {
                        this.groupData = config.get("group");
                    }
                    if (config.containsKey("printName")) {
                        this.printNameData = config.get("printName");
                    }
                    if (config.containsKey("printMode")) {
                        this.printModeData = config.get("printMode");
                    }
                }
            }
        } catch (Exception ex) {

        }
    }

    public static void setInstance(OpcUaConfig instance) {
        OpcUaConfig.instance = instance;
    }

    private static OpcUaConfig instance;

    public static OpcUaConfig getInstance() {
        return instance;
    }

    public OpcUaConfig() {
        instance = this;
    }

}
